<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="public/exts/xcrud/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<table class="table table-bordered table-stripped">
	<thead>
    	<tr>
        	<td>STT</td>
            <td>Tên Function Ext</td>
            <td>Explain</td>
        
        </tr>
    </thead>
    
    <tbody>
    	<tr>
        	<td>#1</td>
            <td>clear_the_loai_fk </td>
            <td>Query list anime id trong table anime_Theloai_fk. Sau đó query lại table anime, nếu id không tồn tại , clear dữ liệu trong Table anime_theloai_fk</td>
        
        </tr>
        
        <tr>
        	<td>#2</td>
            <td>theloai | Id desc , có start và end </td>
            <td>Thay thế, Upadate lại thể loại của anime</td>
        
        </tr>
        
          <tr>
        	<td>#3</td>
            <td>theloai_insert | Id desc , có start và end</td>
            <td>Insert theloai của anime vào table anime_theloai_fk</td>
        
        </tr>
          <tr>
        	<td>#4</td>
            <td>update_follow | Id desc , có start và end</td>
            <td>Update lại số lượt follow của anime</td>
        
        </tr>
          <tr>
        	<td>#5</td>
            <td>nguon | Id desc , có start và end</td>
            <td>Thêm Nguồn mới vào anime_fansub</td>
        
        </tr>
    </tbody>

</table>

<?php

function manga_theloai($id,$str){
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											
												if($value!=''){
												$tag=khongdau(trim($value));
												$db->query("select * from anime_theloai where alias ='".$tag."'");
											
														if($db->num_rows()>0){
															$row=$db->fetchOne();
														
															
															//Handle tag_post
															$db->query('select * from anime_theloai_fk where anime_id='.$id.' and theloai_id='.$row['id']);
														
																if($db->num_rows()>0){
																
																}else{
																	
																 	$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$row['id'].",".$id.")");
															
																}
														
														}else{
														
															$db->nonquery("insert into anime_theloai(name,alias) values('".$value."','".$tag."')");
															$tag_id=$db->get_insert_id();
															$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$tag_id.",".$id.")");
															
															
														}
												}//End of $value!=''
												
												//End code moi
										}
										
						$db->close();
							
				}

}

$val=$_GET['ext'];
switch($val){
	/*
	case 'running':
		$id = $_GET['id'];
		$model = new model();
		$model->where = "id >= '" . $id . "'" ;
		$model->delete('anime');
		
		$model->where = "manga >= '" . $id . "'";
		$model->delete('chapterk2');
		
		break;
	*/
	case "nguon":
					$start=$_GET['start'];
					$end=$_GET['end'];
					$db=new Model;
					$db->query("select id,name,nguon from anime order by id desc limit $start,$end");
					$data=$db->fetchAll();
					foreach($data as $row){
						if($row['nguon']!=''){
							//Find website and replace name
							
							//Manga24h,vnsharing,vnsharing.net,blogtruyen,truyentranhtuan,truyentranh8,vechai,truongton.net
								
							
							
							$row['nguon']=str_replace('|',',',$row['nguon']);
									$str=$row['nguon'];
									if(trim($str)!=''){
										$theloaiar=explode(",",$str);
									}
									/*
									$str2=implode(',',$theloaiar);
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo $str2." - ".$row['name']."<Br/>";
									*/
									
									if($str!='')
									foreach($theloaiar as $value ){
										//Kiem tra the loai nay da co trong talbe anime_fansub chua
										$db->query("select id from anime_fansub where alias like '".khongdau($value)."'");
										if($db->num_rows()==0){
											$dt=array("name"=>$value,"alias"=>khongdau($value));
											$db->insert("anime_fansub",$dt);
										}
											
									}
									
						} //End of if nguon !=''
					} //End foreach data as row
					
					
					
			break;
			//Clear anime thừa trong table anime_theloai_fk
			case "clear_theloai_fk":
					$db=new Model;
					$db->query("select anime_id from anime_theloai_fk");
					$data=$db->fetchAll();
					foreach($data as $val){
					$db->query("select id from anime where id=".$val['anime_id']);
						if($db->num_rows()==0){
							echo $val['anime_id']." - ";
							$db->nonquery("delete from anime_theloai_fk where anime_id=".$val['anime_id']);
						}
					}
					break;
		
			case "theloai":
			//Fix lại thể loại của anime, và update lại row the loai
					$start=$_GET['start'];
					$end=$_GET['end'];
					$db=new Model;
					$db->query("select id,name,theloai from anime order by id desc limit $start,$end");
					$data=$db->fetchAll();
					$j=0;
					foreach($data as $row){
						if($row['theloai']!=''){
							//Find website and replace name
							$val=$row['theloai'];
							$row['theloai']=str_ireplace('sci fi','Sci-fi',$row['theloai']);
							$row['theloai']=str_ireplace('sci-fi','Sci-fi',$row['theloai']);
							$row['theloai']=str_ireplace('manga','Manga',$row['theloai']);
							$row['theloai']=str_ireplace('webtoons','Webtoon',$row['theloai']);
							$row['theloai']=str_ireplace('webtoon','Webtoons',$row['theloai']);
							$row['theloai']=str_ireplace('martial arts','Martial Art',$row['theloai']);
							$row['theloai']=str_ireplace('martial art','Martial Arts',$row['theloai']);
							$row['theloai']=str_ireplace('sports','Sport',$row['theloai']);
							$row['theloai']=str_ireplace('sport','Sports',$row['theloai']);
							$row['theloai']=str_ireplace('Shonen','Shounen',$row['theloai']);
							$row['theloai']=str_ireplace('Shounen-ai','Shounen Ai',$row['theloai']);
							$row['theloai']=str_ireplace('history','Historical',$row['theloai']);
							$row['theloai']=str_ireplace('one shot','Oneshot',$row['theloai']);
							$row['theloai']=str_ireplace('Supernaltura','Supernatural',$row['theloai']);
							//Manga24h,vnsharing,vnsharing.net,blogtruyen,truyentranhtuan,truyentranh8,vechai,truongton.net
							$row['theloai']=ucwords($row['theloai']);
							echo $j++." | ".$row['name'].":<Br/>".$val." =>".$row['theloai']."<Br/>";	
							$row['theloai']=ucwords($row['theloai']);
							$db->nonquery("update anime set theloai='".$row['theloai']."' where id=".$row['id'] );
								/*
								$row['nguon']=str_replace('|',',',$row['nguon']);
									$str=$row['nguon'];
									if(trim($str)!=''){
										$theloaiar=explode(",",$str);
									}
									/*
									$str2=implode(',',$theloaiar);
									echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
									echo $str2." - ".$row['name']."<Br/>";
								
									
									if($str!='')
									foreach($theloaiar as $value ){
										//Kiem tra the loai nay da co trong talbe anime_fansub chua
										$db->query("select id from anime_fansub where alias like '".khongdau($value)."'");
										if($db->num_rows()==0){
											$dt=array("name"=>$value,"alias"=>khongdau($value));
											$db->insert("anime_fansub",$dt);
										}
											
									}
									*/
									
						} //End of if nguon !=''
					} //End foreach data as row
					
					
					
			break;	
				case "theloai_insert":
				//Insert the loai vao table anime_theloai_fk
					$start=$_GET['start'];
					$end=$_GET['end'];
					$db=new Model;
					$db->query("select id,name,theloai from anime order by id desc limit $start,$end");
					$data=$db->fetchAll();
					$j=0;
					foreach($data as $row){
						if($row['theloai']!=''){
						$row['theloai']=str_ireplace('Slice_Of_Life','Slice Of Life',$row['theloai']);
						$row['theloai']=str_ireplace('School_life','School Life',$row['theloai']);
							$str=$row['theloai'];
							//--------------------------------- Start insert --------
									$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim(ucwords($value));
											
											
												if($value!=''){
												$tag=khongdau($value);
												$db->query("select * from anime_theloai where alias ='".$tag."'");
											
														if($db->num_rows()>0){
															$row2=$db->fetchOne();
														
															
															//Handle tag_post
															$db->query('select * from anime_theloai_fk where anime_id='.$row['id'].' and theloai_id='.$row2['id']);
														
																if($db->num_rows()>0){
																
																}else{
																	
																 	$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$row2['id'].",".$row['id'].")");
															
																}
														
														}else{
														
															$db->nonquery("insert into anime_theloai(name,alias) values('".$value."','".$tag."')");
															$tag_id=$db->get_insert_id();
															$db->nonquery("insert into anime_theloai_fk(theloai_id,anime_id) values(".$tag_id.",".$row['id'].")");
															
															
														}
												}//End of $value!=''
												
												//End code moi
										}
							
							//---------------------------------- End insert ---------
									
						} //End of if nguon !=''
					} //End foreach data as row
					
					
					
			break;		
			
		case "update_follow":
			$start=$_GET['start'];
			$end=$_GET['end'];
			$md=new Model;
			$sql="select id from anime order by id desc limit $start,$end";
			$md->query($sql);
			$res=$md->fetchAll();
			foreach($res as $val){
				$md->query("select id from truyen_history where truyen_id =".$val['id']);
				$num=$md->num_rows();
				$md->nonquery("update anime set follows=".$num." where id=".$val['id']);
			}
			break;			
					
					
}

exit();